package com.example.liuyuming_

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable


class MainActivity : AppCompatActivity() {
    private var editTextNumber1: EditText? = null
    private var editTextNumber2: EditText? = null
    private var textViewResult: TextView? = null
    internal var historyList: MutableList<String> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "刘渝铭2022110324"

        editTextNumber1 = findViewById(R.id.editTextNumber1)
        editTextNumber2 = findViewById(R.id.editTextNumber2)
        textViewResult = findViewById(R.id.textViewResult)

        loadHistory()

        val buttonAdd = findViewById<Button>(R.id.buttonAdd)
        buttonAdd.setOnClickListener { calculate("+") }

        val buttonSubtract = findViewById<Button>(R.id.buttonSubtract)
        buttonSubtract.setOnClickListener { calculate("-") }

        val buttonMultiply = findViewById<Button>(R.id.buttonMultiply)
        buttonMultiply.setOnClickListener { calculate("*") }

        val buttonDivide = findViewById<Button>(R.id.buttonDivide)
        buttonDivide.setOnClickListener { calculate("/") }

        val buttonHistory = findViewById<Button>(R.id.buttonHistory)
        buttonHistory.setOnClickListener {
            // 将 historyList 作为 Serializable 对象传递给 HistoryActivity
            val intent = Intent(this, HistoryActivity::class.java)
            intent.putExtra("HISTORY_LIST", historyList as Serializable)
            startActivity(intent)
        }
    }

    private fun calculate(operation: String) {
        try {
            val num1 = editTextNumber1!!.text.toString().toDouble()
            val num2 = editTextNumber2!!.text.toString().toDouble()
            var result = 0.0
            when (operation) {
                "+" -> result = num1 + num2
                "-" -> result = num1 - num2
                "*" -> result = num1 * num2
                "/" -> if (num2 != 0.0) {
                    result = num1 / num2
                } else {
                    textViewResult!!.text = "Cannot divide by zero"
                    return
                }
            }
            textViewResult!!.text = result.toString()
            historyList.add(String.format("%.2f %s %.2f = %.2f", num1, operation, num2, result))
            saveHistory()
        } catch (e: NumberFormatException) {
            textViewResult!!.text = "Invalid input"
        }
    }

    private fun saveHistory() {
        try {
            ObjectOutputStream(FileOutputStream(HISTORY_FILE)).use { out ->
                out.writeObject(historyList)
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    private fun loadHistory() {
        try {
            ObjectInputStream(FileInputStream(HISTORY_FILE)).use { `in` ->
                historyList = `in`.readObject() as MutableList<String>
            }
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: ClassNotFoundException) {
            e.printStackTrace()
        }
    }

    companion object {
        private const val HISTORY_FILE = "history.dat"
    }
}